<?php defined('BASEPATH') or exit('No direct script access allowed');

class Account_m extends MY_Model {
	
	function __construct(){
		parent::__construct();
		$this->load->library('module_helper');
	}
	
	function index(){
		$USER = $this->user_entity->init('id',$_SESSION['user']['id']);
		return $USER;
	}
	
	function submit_information(){
		 
		$USER = $this->user_entity->init('id',$_SESSION['user']['id']);
		//get old data
		$revision = $USER->buildNewRevision();
		
		$USER->setFirstName( $this->input->post('first_name','') );
		$USER->setLastName( $this->input->post('last_name','') );
		$USER->setJobTitle( $this->input->post('job_title','') );
		$USER->setCompany( $this->input->post('company','') );
		$USER->setCountry( $this->input->post('country','') );
		$USER->setAddressLine1( $this->input->post('address_line_1','') );
		$USER->setAddressLine2( $this->input->post('address_line_2','') );
		$USER->setCity( $this->input->post('city','') );
		$USER->setState( $this->input->post('state','') );
		$USER->setZip( $this->input->post('zip','') );
		$USER->setWorkPhone( $this->input->post('work_phone','') );
		$USER->setMobile( $this->input->post('mobile','') );
	//	$USER->setBankNameAcc( $this->input->post('bank_name_acc','') );
	//	$USER->setBankAccNo( $this->input->post('bank_acc_no','') );
	//	$USER->setBankName( $this->input->post('bank_name','') );
	//	$USER->setBankAddress( $this->input->post('bank_address','') );
	//	$USER->setSwiftCode( $this->input->post('swift_code','') );
		$USER->setRevision($revision);
		$USER->update();
		
		$this->crontab_email->cronUpdate( $USER->getAccCode(),'_sendEmailToUser_editAccNotify', $USER->getId(), array(time()) );
			
		echo 'ok';
		exit();
	}
	
	function submit_password(){
		$USER = $this->user_entity->init('id',$_SESSION['user']['id']);
		$revision = $USER->buildNewRevision();
		
		$USER->setPassword( $this->input->post('new_password','') );
		$USER->setRevision($revision);
		
		$USER->update();
			
		echo 'ok';
		exit();
	}
	
	function submit_email(){
		$USER = $this->user_entity->init('id',$_SESSION['user']['id']);
		$newEmail = $this->input->post('new_email','');
		
		if($USER->getEmail() == $newEmail){
			echo 'ok';exit();
		}else{
			$OTHER_USER = $this->user_entity->init('email',$newEmail);
			if($OTHER_USER){
				echo 'err#This is email  already existed in system. You can not use this email.';exit();
			}else{
				$revision = $USER->buildNewRevision();
				
				$USER->setEmail($newEmail);
				$USER->setRevision($revision);
				
				$USER->update();
				$this->crontab_email->cronUpdate( $USER->getAccCode(),'_sendEmailToUser_editAccNotify', $USER->getId(), array(time()) );
			
				echo 'ok';
				exit();
			}
		}
	} 
	
	function buyer_information(){
		$buyer_acc_code = $this->uri->segment(3,'');
		$BUYER = $this->user_entity->init('acc_code', $buyer_acc_code);
		
		if($BUYER->getAccType() != 'buyer'){
			show_404();exit();
		}
		$bid_won_records = $this->db->select("BID.*")->
							from(TBL_BID." AS BID")->
							join(TBL_SHOW." AS SHOWTBL","BID.show_ref_no=SHOWTBL.ref_no")->
							join(TBL_ACCOUNT." AS ACCTBL","BID.account_code=ACCTBL.acc_code")->
							where( array( 'SHOWTBL.user_bid_won'=> $BUYER->getId(), 'ACCTBL.id'=> $BUYER->getId() ))->get()->result();
		return array('buyer'=>$BUYER,'bid'=>$bid_won_records);
	}
	
}